package com.easydev.basic.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import com.easydev.basic.common.exception.DemoException;

/**
 * 自定义异常处理类
 * 
 * @author glf
 * @version 1.0
 * @CreateDate 2016年3月17日
 */
public class CommonExceptionResolver implements HandlerExceptionResolver {

    private Logger log = LoggerFactory.getLogger(CommonExceptionResolver.class);

    /**
     * 当controller层抛出异常时，这边会拦截到异常，便于对异常统一处理
     * 
     * @author glf
     * @param request
     * @param response
     * @param handler
     * @param exception
     * @return
     */
    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception exception) {
        log.info("CommonExceptionResolver resolveException执行，处理异常", exception);
        if (exception instanceof DemoException) {
            return new ModelAndView("demo/index");
        }
        return new ModelAndView("common/500");
    }

}
